package com.rem.redis.controller;

import cn.hutool.core.util.RandomUtil;
import com.rem.redis.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Rem
 * @date 2022-05-03
 */

@RestController
public class SeckillController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    SeckillService seckillService;

    @GetMapping("/seckill/{productId}")
    public String seckill(@PathVariable Integer productId) {
        String userId = RandomUtil.randomNumbers(6);
        boolean result = seckillService.seckill(userId, productId);
        if (result) {
            return "秒杀成功";
        }
        return "秒杀失败";
    }


    @GetMapping("/doSecKill/{productId}")
    public String doSecKill(@PathVariable String productId) {
        String userId = RandomUtil.randomNumbers(6);
        boolean result = seckillService.doSecKill(userId, productId);

        if (result) {
            return "秒杀成功";
        }
        return "秒杀失败";
    }


    @GetMapping("/doSecKillWithScript/{productId}")
    public String doSecKillWithScript(@PathVariable String productId) {
        String userId = RandomUtil.randomNumbers(6);
        boolean result = seckillService.doSecKillWithScript(userId, productId);
        if (result) {
            return "秒杀成功";
        }
        return "秒杀失败";
    }
}
